package com.sisy.demo.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import org.springframework.jdbc.datasource.init.DatabasePopulator;
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;


@Configuration
@EnableTransactionManagement
@PropertySource(ignoreResourceNotFound = true, value = { "classpath:jdbc.properties" })
@MapperScan("com.sisy.demo.dao")
public class H2DataSourceTestConfig {


//    @Value("${spring.datasource.username}")
    private String username = "sa";

//    @Value("${spring.datasource.password}")
    private String password = "sa";

//    @Value("${spring.datasource.url}")
    private String url = "jdbc:h2:mem:demo_data;MODE=MYSQL;DB_CLOSE_DELAY=-1;";

    @Value("classpath:/db/schema.sql")
    private Resource schemaScript;

//    @Value("classpath:/db/gaotu/INIT_DATA.sql")
//    private Resource dataScript;


    public DataSource gaotuDatasource() throws Exception {
        DriverManagerDataSource dataSource = new DriverManagerDataSource(url, username, password);

        return dataSource;
    }

    @Bean(name = "gaotuDatasource")
    public DataSource getDataSource() throws Exception {
        DataSource dataSource = gaotuDatasource();
//        DatabasePopulatorUtils.execute(databasePopulator(), dataSource);
        return dataSource;
    }

    @Bean(name = "gaotuSessionFactory")
    public SqlSessionFactory sqlSessionFactoryBean(
            @Autowired @Qualifier(value = "gaotuDatasource") final DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
//        sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("db/mybatis-config.xml"));
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean.getObject();
    }

    private DatabasePopulator databasePopulator() {
        final ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
        populator.addScript(schemaScript);
//        populator.addScript(dataScript);
        return populator;
    }

    @Bean(name = "gaotuTransactionManager")
    public PlatformTransactionManager gaotuTransactionManager(
            @Autowired @Qualifier(value = "gaotuDatasource") final DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

}
